package com.agrimall.servlet.admin;

import com.agrimall.entity.User;
import com.agrimall.service.UserService;
import com.agrimall.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 管理员修改密码Servlet
 */
@WebServlet("/admin/user/changePassword")
public class AdminChangePasswordServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("admin");
        
        // 验证管理员登录状态
        if (admin == null) {
            out.print("{\"success\":false,\"message\":\"请先登录\"}");
            out.flush();
            return;
        }
        
        try {
            String oldPassword = request.getParameter("oldPassword");
            String newPassword = request.getParameter("newPassword");
            String confirmPassword = request.getParameter("confirmPassword");
            
            // 验证参数
            if (oldPassword == null || oldPassword.trim().isEmpty()) {
                out.print("{\"success\":false,\"message\":\"请输入当前密码\"}");
                out.flush();
                return;
            }
            
            if (newPassword == null || newPassword.trim().isEmpty()) {
                out.print("{\"success\":false,\"message\":\"请输入新密码\"}");
                out.flush();
                return;
            }
            
            if (confirmPassword == null || confirmPassword.trim().isEmpty()) {
                out.print("{\"success\":false,\"message\":\"请确认新密码\"}");
                out.flush();
                return;
            }
            
            if (!newPassword.equals(confirmPassword)) {
                out.print("{\"success\":false,\"message\":\"新密码和确认密码不一致\"}");
                out.flush();
                return;
            }
            
            if (newPassword.length() < 6) {
                out.print("{\"success\":false,\"message\":\"新密码长度至少6位\"}");
                out.flush();
                return;
            }
            
            // 执行修改密码
            boolean success = userService.changePassword(admin.getUserId(), oldPassword, newPassword);
            
            if (success) {
                out.print("{\"success\":true,\"message\":\"密码修改成功！\"}");
            } else {
                out.print("{\"success\":false,\"message\":\"密码修改失败，当前密码错误或操作失败\"}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            out.print("{\"success\":false,\"message\":\"密码修改失败：" + e.getMessage() + "\"}");
        } finally {
            out.flush();
            out.close();
        }
    }
}



